﻿using System;
using System.Text;
using Core.Domain.Lexems;

namespace Core.SyntaxAnalysis.Classes
{
    /// <summary> Операции AND и OR </summary>
    public class BoolBinary : Expr
    {
        private readonly Expr m_left;
        private readonly Expr m_right;

        public BoolBinary(Lexem lex, Expr left, Expr right)
        {
            Lex = lex;
            m_left = left;
            m_right = right;
        }

        public override DataType GetExprType()
        {
            if (Type == null)
            {
                if (m_left.GetExprType() is Boolean && m_right.GetExprType() is Boolean)
                {
                    Type = new Boolean();
                }
                else
                {
                    throw new Exception("Для операции " + Lex.StrValue + " оба аргумента должны иметь тип Boolean");
                }
            }
            return Type;
        }

        public override string ExprGenerate(StringBuilder s)
        {
            string l = m_left.Generate(s);
            string r = m_right.Generate(s);
            string v = Env.NextVariable();
            s.AppendLine(v + " = " + l + " " + Lex.StrValue + " " + r);
            return v;
        }
    }
}